package com.hlt.controller.user;

import com.hlt.dto.CommentsDTO;
import com.hlt.dto.CommentsPageQueryDTO;
import com.hlt.result.PageResult;
import com.hlt.result.Result;
import com.hlt.service.ICommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzf
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/users/comments")
@Api(tags = "评论接口")
@Slf4j
public class CommentsController {

    @Resource
    private ICommentsService commentsService;

    @PostMapping
    @ApiOperation("发表评论")
    public Result addComment(@RequestBody CommentsDTO commentsDTO) {
        log.info("发表评论：{}", commentsDTO);
        commentsService.addComment(commentsDTO);
        return Result.success();
    }

    @DeleteMapping("/{commentId}")
    @ApiOperation("删除评论")
    public Result deleteComment(@PathVariable Integer commentId) {
        log.info("删除评论：{}", commentId);
        commentsService.deleteComment(commentId);
        return Result.success();
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("获取用户的评论列表")
    public Result<PageResult> getUserComments(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取用户评论列表，用户ID：{}", userId);
        CommentsPageQueryDTO pageQueryDTO = new CommentsPageQueryDTO();
        pageQueryDTO.setUserId(userId);
        pageQueryDTO.setPageNum(pageNum);
        pageQueryDTO.setPageSize(pageSize);
        PageResult pageResult = commentsService.getUserComments(pageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/post/{postId}")
    @ApiOperation("获取文章的评论列表")
    public Result<PageResult> getPostComments(
            @PathVariable Integer postId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取文章评论列表，文章ID：{}", postId);
        CommentsPageQueryDTO pageQueryDTO = new CommentsPageQueryDTO();
        pageQueryDTO.setPostId(postId);
        pageQueryDTO.setPageNum(pageNum);
        pageQueryDTO.setPageSize(pageSize);
        PageResult pageResult = commentsService.getPostComments(pageQueryDTO);
        return Result.success(pageResult);
    }
}
